package cn.haoxsf.app.system.service.impl;

import cn.haoxsf.app.common.api.enums.OrderExceptionEnum;
import cn.haoxsf.app.common.exception.BaseException;
import cn.haoxsf.app.system.entity.Order;
import cn.haoxsf.app.system.entity.OrderRelation;
import cn.haoxsf.app.system.mapper.OrderRelationMapper;
import cn.haoxsf.app.system.service.IOrderRelationService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.log4j.Log4j2;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 接单关系表 服务实现类
 * </p>
 *
 * @author Ele  qvbanfm@qq.com
 * @since 2020-03-20
 */
@Service
@Log4j2
public class OrderRelationServiceImpl extends ServiceImpl<OrderRelationMapper, OrderRelation> implements IOrderRelationService {

    /**
     * 创建接单关系（接单）
     *
     * @param memberId 用户id 必须是师傅
     * @param orderId  订单id
     */
    @Override
    public void createRelation(Long memberId, String orderId) {
        OrderRelation orderRelation = new OrderRelation();
        orderRelation.setMemberId(memberId);
        orderRelation.setOrderId(orderId);
        try{
            baseMapper.insert(orderRelation);
        }catch (RuntimeException e){
            log.error("重复接单；用户id[{}],订单id[{}]",memberId,orderId);
            throw new BaseException(OrderExceptionEnum.YOU_HAVE_TAKEN_THIS_ORDER);
        }

    }

    /**
     * 查询已接订单的关联
     *
     * @param page     分页
     * @param memberId 用户id
     * @return 结果
     */
    @Override
    public List<Map<String, Object>> GetToOrder(Page<OrderRelation> page, Long memberId) {
        return baseMapper.GetToOrder(page,memberId);
    }


}
